package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * <新闻 - 奥一网> 解析类
 *
 * @author KK
 *
 */
public class OeeeeNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		if(url.indexOf("channel")>0){
			return false;
		}
		String[] regex={
				"http://.*.oeeee.com/Content/[0-9]+.htm",
				"http://[a-z]*.oeeee.com/.*/[0-9]{8}/[0-9]*.html",
				"http://[a-z]*.oeeee.com/[a-z]*/[0-9]*/[0-9]*/[0-9]*.html",
				"http://[a-z]*.oeeee.com/[a-z]*/[a-z]*/userid/[0-9]*.html",
				"http://ndphotos.oeeee.com/album/[0-9]{6}/[0-9]{2}/[0-9]*.html",
				"http://[a-z]*.oeeee.com/html/[0-9]{6}/[0-9]{2}/[0-9]*.html",
				"http://epaper.oeeee.com/.*/html/.*content_[0-9].*.htm",
				"http://baoliao.oeeee.com/index/show/id/[0-9]+.html",
				"http://baoliao.oeeee.com/[0-9]*.html",
				"http://gcontent.oeeee.com/[0-9/a-z]*/Blog/[0-9/a-z]*.html",
				"http://baoliao.oeeee.com/index/personal/userid/313294.html",
			};

		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select(".side").remove();

		if(url.startsWith("http://baoliao.oeeee.com")){
			title=doc.select("h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.main-content"));
			dateStr=doc.select("div.main-des").text();
		}else{
			title=doc.select("div.content > div.clearfix > div.left-cont > div.title > a").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content > div.clearfix > div.left-cont > div.cont-main"));
			dateStr=doc.select("div.content > div.clearfix > div.left-cont > div.title-s > span.c-sub").text();

		}
			if(StringUtils.isNullOrEmpty(title)){
				title=doc.select("div.title > a").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.cont-main"));
				dateStr=doc.select("div.title-s > span.c-sub").text();
			}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "table.allline > tr > td > table > tr > td.font_26_333333_h40_center").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "table.allline > tr > td > table > tr > td > div.wqbcontentcss > founder-content"));
			dateStr=doc.select( "table.allline > tr > td > table > tr > td.font_12_666666_h26_center").text();
//			commnetNum=doc.select( "div.content > div.clearfix > div.left-cont > div.title-s > span").text();
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "div.mainbody > div.main > div.article > div.hd > h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "div.mainbody > div.main > div.article > div.bd"));
			dateStr=doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fl").text();
//			clickNum= doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fr").text();
//			commnetNum=doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fr > a ").text();

		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "div.pw h1").text();
			dateStr=doc.select( "div.explain div.hd div.key").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "div.explain div.bd"));
//			clickNum= doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fr").text();
//			commnetNum=doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fr > a ").text();

		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "div.listshow h2").text();
			dateStr=doc.select( "div.listshow div.time").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "div.listshow div.text p"));
//			clickNum= doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fr").text();
//			commnetNum=doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fr > a ").text();

		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "div.hd h1").text();
			dateStr=doc.select( "div.hd div.info").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "div.hd div.bd"));
//			clickNum= doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fr").text();
//			commnetNum=doc.select( "div.mainbody > div.main > div.article > div.hd > div.info > div.fr > a ").text();

		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "div.hd h3#hTitle").text();
			dateStr=doc.select( "div.hd div.sx").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "div#divContent"));
			clickNum= doc.select( "div.hd div.sx span:eq(0)").text();
			commnetNum=doc.select( "div.hd div.sx span:eq(1)").text();

		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "div.titleleft span.c-main").text();
			dateStr=doc.select( "div.titleleft").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "div.f14px"));
		}
		date=DateUtils.matchDate(dateStr);

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "div.cont-tit").text();
			dateStr=doc.select( "span.c-sub").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "div.cont-txt"));
			date=DateUtils.matchDate(dateStr);
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "div.dbt").text();
			dateStr=doc.select( "div.dbtex").text();
			content=HtmlCleaner.getContentHtml(url,doc.select( "div.text"));
			date=DateUtils.matchDate(dateStr);
		}
		
		if(date==null)
		{
			date = DateUtils.matchDate(doc.select("div.main-info").text());
		}
		if(date == null)
		{
			date = DateUtils.matchDate(doc.select("span#pubtime_baidu").text());
		}
		if(date == null)
		{
			date = DateUtils.matchDate(doc.select("div.info").text());
		}
		if(date == null){
			date = DateUtils.matchDate(doc.select("span.time").text());}
		
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select( "title").text();
			title = StringUtils.substringBefore(title, "-");
		}
		
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
		}

			NewsMeta news=new NewsMeta();
			news.setUrl(url);
			news.setType(1);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);

			String author=null;
			author=JSoupUtils.matchAuthor(doc, "来源:","稿源:");
			if(StringUtils.isBlank(author)){
				author=doc.select("div.gcy-img div.wzn-per div.c-main").text();
			}
			if(StringUtils.isBlank(author)){
				Elements authorList = doc.select("a.nickname");
				if(authorList.size()>1)
				{
					author = authorList.get(1).text();
				}
			}
			
			news.setAuthor(author);
			return news;
	}

	public static void main(String[] args) {
		OeeeeNewsAnalyse a=new OeeeeNewsAnalyse();

		String url="http://www.oeeee.com/html/201605/11/393485.html";

		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
